/*
 * @(#)JdbcDao.java Apr 28, 2007
 * 
 * Copyright 2007 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */

package com.cnc.bas.db;
import java.util.List;
import java.util.Map;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.cnc.bas.query.PageInfo;
import com.cnc.bas.query.QuerySql;


/**
 * 
 * @author 阿闲
 * @date   Apr 30, 2007
 * @version 1.0
 */
public class JdbcDao extends JdbcDaoSupport {

	public static final String BEAN_ID = "jdbcDao";
	
public void queryForPage(PageInfo page, QuerySql qs){
		
		StringBuffer limitSql = new StringBuffer(qs.getSql());
		limitSql.append(" limit ");
		limitSql.append((page.getCurrentPage()-1)*page.getPageSize());
		limitSql.append(",");
		limitSql.append(page.getPageSize());
		
		//long start = System.currentTimeMillis();
		if(qs.getValues().length == 0){
			page.setPageData(this.queryForList(limitSql.toString()));
		}else{
			page.setPageData(this.queryForList(limitSql.toString(), qs.getValues()));
		}		
		//long end = System.currentTimeMillis();		
		//System.out.println("query data use time: " + (end - start));
		
		StringBuffer totalSql = new StringBuffer("select count(*) from (");
		totalSql.append(qs.getSql());
		totalSql.append(")temp_table");
		//start = System.currentTimeMillis();
		if(qs.getValues().length == 0){
			page.setTotalCount(this.queryForLong(totalSql.toString()));
		}else{
			page.setTotalCount(this.queryForLong(totalSql.toString(), qs.getValues()));
		}
		//end = System.currentTimeMillis();
		//System.out.println("query total use time: " + (end - start));
		
	}
	
	public int update(String sql){
		return this.getJdbcTemplate().update(sql);
	}
	
	public int update(String sql, Object[] values){
		return this.getJdbcTemplate().update(sql, values);
	}
	
	public int update(String sql, Object[] values, int[] types){
		return this.getJdbcTemplate().update(sql, values, types);
	}
	
	public void execute(String sql){
		this.getJdbcTemplate().execute(sql);
	}
	
	public long queryForLong(String sql){
		return this.getJdbcTemplate().queryForLong(sql);
	}
	
	public long queryForLong(String sql, Object[] values){
		return this.getJdbcTemplate().queryForLong(sql, values);
	}
	
	public long queryForLong(String sql, Object[] values, int[] types){
		return this.getJdbcTemplate().queryForLong(sql, values, types);
	}
	
	public Object queryForObject(String sql,Class rtnClass) {
	    return this.getJdbcTemplate().queryForObject(sql,rtnClass);
	}

	
	public Map queryForMap(String sql){
		
		try {			
			return this.getJdbcTemplate().queryForMap(sql);			
		} catch (IncorrectResultSizeDataAccessException e) {			
			return null;
		}
	}
	
	public Map queryForMap(String sql, Object[] values){
		
		try {
			return this.getJdbcTemplate().queryForMap(sql, values);
		} catch (IncorrectResultSizeDataAccessException e) {
			return null;
		}
		
	}
	
	public Map queryForMap(String sql, Object[] values, int[] types){
		
		try {
			return this.getJdbcTemplate().queryForMap(sql,values,types);
		} catch (IncorrectResultSizeDataAccessException e) {
			return null;
		}		
	}
	
	public List queryForList(String sql){
		return this.getJdbcTemplate().queryForList(sql);
	}
	
	public List queryForList(String sql, Object[] values){
		return this.getJdbcTemplate().queryForList(sql, values);
	}
	
	public int[] batchUpdate(String[] sqls){
		return this.getJdbcTemplate().batchUpdate(sqls);
	}
	
	public int[] batchUpdate(String sql,BatchPreparedStatementSetter statementSetter){
		return this.getJdbcTemplate().batchUpdate(sql,statementSetter);
	}
	
	public void batchUpdate(List<String> sqls){
		
		if(sqls.size() == 0) return;
		String[] strSqls = new String[sqls.size()];
		sqls.toArray(strSqls);
		this.batchUpdate(strSqls);
		
	}

	
	
}
